<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;
use app\admin\model\Article as ArticleModel;

class Article extends Base
{
	public function lst()
	{	
		// $list = ArticleModel::paginate(10);
		$list = db('article')->alias('a')->join('cate c','c .id=a.cateid')->field('a.id,a.title,a.pic,a.author,a.state,a.keywords,a.click,a.time,c.catename')->paginate(10);
		$this->assign('list',$list);
	    return $this->fetch('lst');
	}
	
	public function edit()
    {
		$id = input('id');
		$articles = db('article')->where('id',$id)->find();
		
		// 修改提交
		if(request()->isPost()){
			$data=[
				'id' => input('id'),
				'title'=>input('title'),
				'author'=>input('author'),
				'time'=>time(),
				'desc'=>input('desc'),
				'keywords'=>input('keywords'),
				// 'pic'=>input('pic'),
				'state'=>input('state'),
				'cateid'=>input('cateid'),
				'content'=>input('content'),
			];
			if($data['state']=='on'){
				$data['state'] = 1;
			}else{
				$data['state'] = 0;
			}
			// 缩略图文件上传
			if($_FILES['pic']['tmp_name']){
				$file = request()->file('pic');
				$info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
				$data['pic'] = 'uploads/'.$info->getSaveName();
			}
			
			// 验证
			$validate = \think\Loader::validate('Article');
			if(!$validate->check($data)){
			    $this->error($validate->getError());
				die;
			}
			
			$save = db('article')->update($data);
			if($save !== false){
				$this->success('修改成功！','lst');
			}else{
				$this->error('修改失败！');
			}
			return;
		}
		
		// 分配数据
		$cateres = db('cate')->select();
		$this->assign('cateres',$cateres);
		$this->assign('articles',$articles);
		
        return $this->fetch();
    }
    
    public function add()
    {	
		if(request()->isPost()){
			// 接收数据
			$data=[
				'title'=>input('title'),
				'author'=>input('author'),
				'time'=>time(),
				'desc'=>input('desc'),
				'keywords'=>input('keywords'),
				'pic'=>'',
				'state'=>input('state'),
				'cateid'=>input('cateid'),
				'content'=>input('content'),
			];
			if($data['state']=='on'){
				$data['state'] = 1;
			}else{
				$data['state'] = 0;
			}
			// 缩略图文件上传
			if($_FILES['pic']['tmp_name']){
				$file = request()->file('pic');
				// dump($file);die;
				$info = $file->move(ROOT_PATH . 'public' . DS . 'static/uploads');
				$data['pic'] = 'uploads/'.$info->getSaveName();
				// dump($info);die;
			}
			
			// 验证
			$validate = \think\Loader::validate('Article');
			if(!$validate->check($data)){
			    $this->error($validate->getError());
				die;
			}
			
			// 判断结果
			if(Db::name('article')->insert($data)){
				return $this->success('添加文章成功','lst');
			}else{
				return $this->error('添加文章失败');
			}
			return;
		}
		$cateres = db('cate')->select();
		$this->assign('cateres',$cateres);
        return $this->fetch('add');
    }
	
	public function del(){
		$id = input('id');
		if(db('article')->delete(input('id'))){
			$this->success('删除文章成功','lst');
		}else{
			$this->error('删除文章失败');
		}
	}
	
	
}
